package Controller;

import entity.Book;
import entity.Borrow;
import entity.Reader;
import service.BookService;
import service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/book")
public class BookServlet extends HttpServlet {

    private BookService bookService=new BookServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Reader reader= (Reader) session.getAttribute("reader");
        String method = req.getParameter("method");
        if(method==null){
            method="findAll";
        }
        switch (method){
            case "findAll":
                String pagestr = req.getParameter("page");
                Integer page=Integer.parseInt(pagestr);
                List<Book> list=null;
                try {
                    list=bookService.findAll(page);
                    req.setAttribute("list",list);
                    req.setAttribute("dataPrePage",6);
                    req.setAttribute("currentPage",page);
                    req.setAttribute("pages",bookService.getPages());
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                req.getRequestDispatcher("index1.jsp").forward(req,resp);
                break;
            case "addBorrow":
                String bookidstr = req.getParameter("bookid");
                Integer bookid=Integer.parseInt(bookidstr);
                try {
                    //借书请求
                    bookService.addBorrow(bookid,reader.getId());
                    resp.sendRedirect("/book?method=findAllBorrow&page=1");
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                /*System.out.println(borrowList);*/
                break;
            case"findAllBorrow":
                pagestr=req.getParameter("page");
                page=Integer.parseInt(pagestr);
                //展示当前用户所有借书记录
                try {
                    List<Borrow> borrowList=bookService.findAllBorrowByReaderId(reader.getId(),page);
                    req.setAttribute("list",borrowList);
                    /*req.getRequestDispatcher("borrow.jsp").forward(req,resp);*/
                    req.setAttribute("dataPrepare",6);
                    req.setAttribute("currentPage",page);
                    req.setAttribute("pages",bookService.getBorrowPages(reader.getId()));
                    req.getRequestDispatcher("borrow.jsp").forward(req,resp);
                } catch (SQLException e) {
                    e.printStackTrace();
                }

        }

    }
}
